export default class Interceptor {
  use (onFulfilled, onRejected) {
    checkFn(onFulfilled)
    checkFn(onRejected)

    this._onFulfilled = onFulfilled
    this._onRejected = onRejected
  }

  intercept (data) {
    try {
      if (!this._onFulfilled) return data
      return this._onFulfilled(data)
    } catch (e) {
      this.onError(e)
      throw e
    }
  }

  onError (data) {
    if (!this._onRejected) return data
    return this._onRejected(data)
  }
}

function checkFn (fn) {
  if (fn && typeof fn !== 'function') {
    throw new Error('use的参数必须是function')
  }
}

